#include "key.h"
unsigned char keynum;
sbit row1 = P3^0;
sbit row2 = P3^1;
sbit row3 = P3^2;
sbit row4 = P3^3;

static void clean(void)
{
	row1 = 1;
	row2 = 1;
	row3 = 1;
	row4 = 1;
}

void scankey(void)
{
	static unsigned char status = 0;
	static unsigned char midvalue = 0;
	static unsigned char num = 0;
	switch(status)
	{
		case 0:
			clean();
			if(row1==0||row2==0||row3==0||row4==0)
			{
				status = 1;
			}
			break;
	  case 1:
			clean();
			if(row1==0){
				midvalue = 7;
				status = 2;
			}else if(row2==0){
				midvalue = 6;
				status = 2;
			}else if(row3==0){
				midvalue = 5;
				status = 2;
			}else if(row4==0){
				midvalue = 4;
				status = 2;
			}else{
				status = 0;
			}
			break;
		case 2:
			clean();
			if(row1==1&&row2==1&&row3==1&&row4==1)
			{
				if(num == 98)
				{
					keynum = 8;
				}
				else
				{
					keynum = midvalue;
				}
				num = 0;
				status = 0;
			}
			else if(row1 == 0 && num < 98)
			{
				num++;
			}
			break;
	}
}